using UnityEngine;
using System.Collections;
using System;
using System.IO;

public class lobby : MonoBehaviour 
{
    //varibles
    public GUISkin defaultSkin;
    public Texture2D background;
    public GameObject wepCam, GunSpawner;
    public string[] Maps , Mods;
    public string[] Wepones;
    public bool[] Secondary;
    public string[] weaponeType;
    public int[] levelUnlockAt;
    public GameObject[] GunToSpawn;
    public bool[] noScope, Reflex, Acog, EOTech, NVG, rifeScope, suppressor, tacticalLight, GrenadLauncher, Bayonet;
    public int[] reflexLevel, AcogLevel, EOTechLEvel, NVGLevel, rifleLevel, suppressorLevel, tacticalLightLevel, GrenadLauncherLevel, BayonetLevel;
    public Material[] camo;
    public int[] CamoLevels;

    public static string gameMode = "", map = "", serverName = "", maxPlayers = "8";

    private string[] PrimarywepTypes = new string[6] { "Assault Rifle", "Battle Rifle", "Sub Machine Gun", "Light Machine Gun", "Shotgun", "Sniper Rifle" };
    private string[] SecondaryWepTypes = new string[3] { "Pistol", "Launcher", "Special" };
    private float rotationX = 0;
    private string lobbyMenu = "Quick Match", Options = "", weaponEditor = "", serverThing = "", errorMessage = "";
    private bool fullScreen = false, resPic = false;
    private int res = -1, resCount = 10, currentHeight = -1, currentWidth = -1;
    private Vector2 scrollWheel = Vector2.zero;
    private bool[] pixelLight = new bool[5] { false, false, false, false, false }, antiAliasing = new bool[4] { false, false, false, false }, texture = new bool[4] { false, false, false, false };

    private void Start()
    {
        gameManager.camos = camo;
        //Network.InitializeServer(32, 25566, true);
        //MasterServer.RegisterHost("Frontier - Multiplayer", "zee595", "Assassination");
        MasterServer.ClearHostList();
        MasterServer.RequestHostList("Frontier - Multiplayer");
        //Set everything up
        currentHeight = Screen.height;
        currentWidth = Screen.width;
        InvokeRepeating("resCounter", 1, 1);
        gameMode = Mods[0];
        map = Maps[0];
        pixelLight[gameManager.pixelLightCount - 1] = true;
        texture[gameManager.antiAliasing - 1] = true;

        if (gameManager.antiAliasing == 0)
            antiAliasing[0] = true;
        else if (gameManager.antiAliasing == 2)
            antiAliasing[1] = true;
        else if (gameManager.antiAliasing == 4)
            antiAliasing[2] = true;
        else if (gameManager.antiAliasing == 8)
            antiAliasing[3] = true;
    }

    private void resCounter()
    {
        if (resCount <= 0)
        {
            resCount = 10;
            resPic = false;
            Screen.SetResolution(currentWidth, currentHeight, fullScreen);
        }

        if (resPic &&resCount > 0)
            resCount--;
    }

    private void OnGUI()
    {
        GUI.skin = defaultSkin;

        GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), background);

        if (GUILayout.Button("Test Scene"))
        {
            Application.LoadLevel("Test");
        }

        if (resPic)
        {
            if (GUILayout.Button("I can see this", GUILayout.Width(200)))
            {
                currentHeight = Screen.height;
                currentWidth = Screen.width;
                resCount = 10;
                resPic = false;
                saveRes();
            }
        }
        else
        {
            if (GUI.Button(new Rect(20, 100, 170, 25), "Quick Match"))
            {
                MasterServer.ClearHostList();
                MasterServer.RequestHostList("Frontier - Multiplayer");
                lobbyMenu = "Quick Match";
            }
            if (GUI.Button(new Rect(20, 130, 170, 25), "Create Match"))
                lobbyMenu = "Create Match";

            if (GUI.Button(new Rect(20, 160, 170, 25), "Profile"))
                lobbyMenu = "Profile";

            if (GUI.Button(new Rect(20, 190, 170, 25), "Weapons"))
                lobbyMenu = "Weapons";

            if (GUI.Button(new Rect(20, 220, 170, 25), "Options"))
                lobbyMenu = "Options";

            if (GUI.Button(new Rect(20, 250, 170, 25), "Rage Quit"))
                Application.LoadLevel("login");

            GUI.Window(0, new Rect(210, 100, Screen.width - 230, Screen.height - 150), windowFunc, lobbyMenu);
        }
    }

    private void saveRes()
    {
        TextWriter write = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/frontier/res.sav");
        write.WriteLine(fullScreen);
        write.WriteLine(res);
        write.Close();
    }

    private void windowFunc(int id)
    {
        switch (lobbyMenu)
        {
            case "Quick Match":
                GUILayout.BeginHorizontal();

                GUILayout.Label("Map", GUILayout.Width((Screen.width - 270) / 5));
                GUILayout.Label("Game Mode", GUILayout.Width((Screen.width - 270) / 5));
                GUILayout.Label("Server Name", GUILayout.Width((Screen.width - 270) / 5));
                GUILayout.Label("Host", GUILayout.Width((Screen.width - 270) / 5));
                GUILayout.Label("Player #", GUILayout.Width((Screen.width - 270) / 5));
                GUILayout.EndHorizontal();
                scrollWheel = GUILayout.BeginScrollView(scrollWheel, GUILayout.Width(Screen.width - 250));
                if (MasterServer.PollHostList().Length != 0)
                {
                    HostData[] host = MasterServer.PollHostList();
                    foreach (var e in host)
                    {
                        GUILayout.Space(5);
                        GUILayout.BeginHorizontal();
                        GUILayout.TextField(e.comment, GUILayout.Width((Screen.width - 270) / 5));
                        GUILayout.TextField("Not Available", GUILayout.Width((Screen.width - 270) / 5));
                        GUILayout.TextField(e.gameName, GUILayout.Width((Screen.width - 270) / 5));
                        if (GUILayout.Button("Join", GUILayout.Width((Screen.width - 270) / 5)))
                        {
                           // inGameLobby.players.Clear();
                           // inGameLobby.playerTeam.Clear();
                           // lobby.map = e.comment;
                           // inGameLobby.serverIpForClient = e.ip.ToString();
                            Network.Connect(e);
                        }
                        GUILayout.TextField(e.connectedPlayers + " / " + e.playerLimit, GUILayout.Width((Screen.width - 270) / 5));
                        GUILayout.EndHorizontal();
                    }

                    GUILayout.Height(30);
                }
                GUILayout.EndScrollView();
                break;

            case "Create Match":
                switch (serverThing)
                {
                    case "":
                        GUILayout.BeginHorizontal();
                        GUILayout.Label("Map", GUILayout.Width((Screen.width - 280) / 5));
                        if (GUILayout.Button(lobby.map, GUILayout.Width((Screen.width - 280) / 5)))
                        {
                            serverThing = "Map";
                        }
                        GUILayout.EndHorizontal();
                        //=============================================
                        GUILayout.BeginHorizontal();
                        GUILayout.Label("Game Mode", GUILayout.Width((Screen.width - 280) / 5));
                        if (GUILayout.Button(gameMode, GUILayout.Width((Screen.width - 280) / 5)))
                        {
                            serverThing = "Mode";
                        }
                        GUILayout.EndHorizontal();
                        //=============================================
                        GUILayout.BeginHorizontal();
                        GUILayout.Label("Server Name", GUILayout.Width((Screen.width - 280) / 5));
                        serverName = GUILayout.TextField(serverName, GUILayout.Width((Screen.width - 280) / 5));
                        GUILayout.EndHorizontal();
                        //=============================================
                        GUILayout.BeginHorizontal();
                        GUILayout.Label("Max Players", GUILayout.Width((Screen.width - 280) / 5));
                        maxPlayers = GUILayout.TextField(maxPlayers, GUILayout.Width((Screen.width - 280) / 5));
                        GUILayout.EndHorizontal();
                        //=============================================
                        if (GUILayout.Button("Create", GUILayout.Width((Screen.width - 280) / 5)))
                        {
                            if (serverName != "" && maxPlayers != "")
                            {
                                try
                                {
                                    Network.InitializeSecurity();
                                    Network.InitializeServer(int.Parse(maxPlayers), 25566, true);
                                    MasterServer.RegisterHost("Frontier - Multiplayer", serverName, lobby.map);
                                    errorMessage = "Server Created";
                                }
                                catch (Exception e) 
                                {
                                    errorMessage = "Error: Could not create the server : \n" + e;
                                }
                            }
                            else
                            {
                                errorMessage = "Please fill all the fields";
                            }
                        }
                        GUILayout.Label(errorMessage);
                        break;
                    case "Mode":
                        foreach (string mody in Mods)
                        {
                            if (GUILayout.Button(mody))
                            {
                                gameMode = mody;
                                serverThing = "";
                            }
                        }
                        break;
                    case "Map":
                        foreach (string m in Maps)
                        {
                            if (GUILayout.Button(m))
                            {
                                map = m;
                                serverThing = "";
                            }
                        }
                        break;
                }
                break;

            case "Profile":

                break;
            case "Weapons":
                if (weaponEditor != "")
                    if (GUILayout.Button("<<", GUILayout.Width(150)))
                    {
                        if (weaponEditor == "P" || weaponEditor == "S")
                            weaponEditor = "";
                        else
                        {
                            bool inType = false;
                            foreach (string c in PrimarywepTypes)
                            {
                                if (c == weaponEditor)
                                {
                                    weaponEditor = "P";
                                    inType = true;
                                }
                            }
                            foreach (string c in SecondaryWepTypes)
                            {
                                if (c == weaponEditor)
                                {
                                    weaponEditor = "S";
                                    inType = true;
                                }
                            }

                            if (!inType)
                            {
                                for (int x = 0; x < weaponeType.Length; x++)
                                {
                                    if (Wepones[x] == weaponEditor)
                                    {
                                        weaponEditor = weaponeType[x];
                                    }
                                }
                            }
                        }
                    }

                for (int x = 0; x < weaponeType.Length; x++)
                {
                    if (weaponEditor == weaponeType[x])
                    {
                        if (GUILayout.Button(Wepones[x], GUILayout.Width(150)))
                        {

                            string[] lines = new string[2];
                            try
                            {
                                TextReader read = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/wep.sav");
                                lines[0] = read.ReadLine();
                                lines[1] = read.ReadLine();
                                read.Close();
                            }
                            catch (Exception) { }
                            TextWriter write = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/wep.sav");
                            if (!Secondary[x])
                            {
                                write.WriteLine(Wepones[x]);
                                write.WriteLine(lines[1]);
                            }
                            else
                            {
                                write.WriteLine(lines[0]);
                                write.WriteLine(Wepones[x]);
                            }
                            write.Close();
                            if (GameObject.FindWithTag("Player") != null)
                                Destroy(GameObject.FindWithTag("Player"));

                            Instantiate(GunToSpawn[x] , GunSpawner.transform.position , Quaternion.identity);
                            rotationX = 0;
                            weaponEditor = Wepones[x];
                        }
                    }
                }

                for (int x = 0; x < weaponeType.Length; x++)
                {
                    if (Wepones[x] == weaponEditor)
                    {
                        GUILayout.Label("Optics", GUILayout.Width(150));
                        if (noScope[x])
                            if (GUILayout.Button("Iron Sight", GUILayout.Width(150)))
                            {
                                GameObject.FindWithTag("Player").GetComponent<Gun>().currentOptic = Gun.optic.IronSight;
                                string line1 = "", line2 = "";
                                try
                                {
                                    TextReader read = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                    line1 = read.ReadLine();
                                    read.ReadLine();
                                    line2 = read.ReadLine();
                                    read.Close();
                                }
                                catch (Exception)
                                {
                                    if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav"))
                                        File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                }
                                TextWriter write = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                write.WriteLine(line1);
                                write.WriteLine("IronSight");
                                write.WriteLine(line2);
                                write.Close();
                            }
                        if (Reflex[x] && gameManager.Level >= reflexLevel[x])
                            if (GUILayout.Button("Reflex", GUILayout.Width(150)))
                            {
                                GameObject.FindWithTag("Player").GetComponent<Gun>().currentOptic = Gun.optic.reflex;
                                string line1 = "", line2 = "";
                                try
                                {
                                    TextReader read = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                    line1 = read.ReadLine();
                                    read.ReadLine();
                                    line2 = read.ReadLine();
                                    read.Close();
                                }
                                catch (Exception)
                                {
                                    if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav"))
                                        File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                }
                                TextWriter write = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                write.WriteLine(line1);
                                write.WriteLine("reflex");
                                write.WriteLine(line2);
                                write.Close();
                            }

                        if (Acog[x] && gameManager.Level >= AcogLevel[x])
                            if (GUILayout.Button("ACOG", GUILayout.Width(150)))
                            {
                                GameObject.FindWithTag("Player").GetComponent<Gun>().currentOptic = Gun.optic.ACOG;
                                string line1 = "", line2 = "";
                                try
                                {
                                    TextReader read = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                    line1 = read.ReadLine();
                                    read.ReadLine();
                                    line2 = read.ReadLine();
                                    read.Close();
                                }
                                catch (Exception)
                                {
                                    if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav"))
                                        File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                }
                                TextWriter write = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                write.WriteLine(line1);
                                write.WriteLine("ACOG");
                                write.WriteLine(line2);
                                write.Close();
                            }

                        if (EOTech[x] && gameManager.Level >= EOTechLEvel[x])
                            if (GUILayout.Button("EOTech", GUILayout.Width(150)))
                            {
                                GameObject.FindWithTag("Player").GetComponent<Gun>().currentOptic = Gun.optic.EOTech;
                                string line1 = "", line2 = "";
                                try
                                {
                                    TextReader read = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                    line1 = read.ReadLine();
                                    read.ReadLine();
                                    line2 = read.ReadLine();
                                    read.Close();
                                }
                                catch (Exception)
                                {
                                    if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav"))
                                        File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                }
                                TextWriter write = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                write.WriteLine(line1);
                                write.WriteLine("EOTech");
                                write.WriteLine(line2);
                                write.Close();
                            }

                        if (NVG[x] && gameManager.Level >= NVGLevel[x])
                            if (GUILayout.Button("Night Vision Scope", GUILayout.Width(150)))
                            {
                                GameObject.FindWithTag("Player").GetComponent<Gun>().currentOptic = Gun.optic.NVG;
                                string line1 = "", line2 = "";
                                try
                                {
                                    TextReader read = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                    line1 = read.ReadLine();
                                    read.ReadLine();
                                    line2 = read.ReadLine();
                                    read.Close();
                                }
                                catch (Exception)
                                {
                                    if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav"))
                                        File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                }
                                TextWriter write = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                write.WriteLine(line1);
                                write.WriteLine("NVG");
                                write.WriteLine(line2);
                                write.Close();
                            }

                        if (rifeScope[x] && gameManager.Level >= rifleLevel[x])
                            if (GUILayout.Button("RifleScope", GUILayout.Width(150)))
                            {
                                GameObject.FindWithTag("Player").GetComponent<Gun>().currentOptic = Gun.optic.RifleScope;
                                string line1 = "", line2 = "";
                                try
                                {
                                    TextReader read = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                    line1 = read.ReadLine();
                                    read.ReadLine();
                                    line2 = read.ReadLine();
                                    read.Close();
                                }
                                catch(Exception)
                                {
                                    if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav"))
                                        File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                }
                                TextWriter write = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                write.WriteLine(line1);
                                write.WriteLine("RifleScope");
                                write.WriteLine(line2);
                                write.Close();
                            }
                        GUILayout.Label("Tactical", GUILayout.Width(150));
                        if (GUILayout.Button("None", GUILayout.Width(150)))
                        {
                            GameObject.FindWithTag("Player").GetComponent<Gun>().currentTactic = Gun.tactical.none;
                            string line1 = "" , line2 = "";
                                try
                                {
                                    TextReader read = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                    line1 = read.ReadLine();
                                    line2 = read.ReadLine();
                                    read.Close();
                                }
                                catch(Exception)
                                {
                                    if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav"))
                                        File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                }
                                TextWriter write = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                write.WriteLine(line1);
                                write.WriteLine(line2);
                                write.WriteLine("none");
                                write.Close();
                        }
                        if (suppressor[x] && gameManager.Level >= suppressorLevel[x])
                            if (GUILayout.Button("Suppressor", GUILayout.Width(150)))
                            {
                                GameObject.FindWithTag("Player").GetComponent<Gun>().currentTactic = Gun.tactical.suppressor;
                                string line1 = "" , line2 = "";
                                try
                                {
                                    TextReader read = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                    line1 = read.ReadLine();
                                    line2 = read.ReadLine();
                                    read.Close();
                                }
                                catch(Exception)
                                {
                                    if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav"))
                                        File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                }
                                TextWriter write = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                write.WriteLine(line1);
                                write.WriteLine(line2);
                                write.WriteLine("suppressor");
                                write.Close();
                            }

                        if (tacticalLight[x] && gameManager.Level >= tacticalLightLevel[x])
                            if (GUILayout.Button("Tactical Light", GUILayout.Width(150)))
                            {
                                GameObject.FindWithTag("Player").GetComponent<Gun>().currentTactic = Gun.tactical.tacticalLight;
                                string line1 = "" , line2 = "";
                                try
                                {
                                    TextReader read = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                    line1 = read.ReadLine();
                                    line2 = read.ReadLine();
                                    read.Close();
                                }
                                catch(Exception)
                                {
                                    if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav"))
                                        File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                }
                                TextWriter write = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                write.WriteLine(line1);
                                write.WriteLine(line2);
                                write.WriteLine("tacticalLight");
                                write.Close();
                            }

                        if (GrenadLauncher[x] && gameManager.Level >= GrenadLauncherLevel[x])
                            if (GUILayout.Button("Grenade Launcher", GUILayout.Width(150)))
                            {
                                GameObject.FindWithTag("Player").GetComponent<Gun>().currentTactic = Gun.tactical.GrenadLauncher;
                                string line1 = "" , line2 = "";
                                try
                                {
                                    TextReader read = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                    line1 = read.ReadLine();
                                    line2 = read.ReadLine();
                                    read.Close();
                                }
                                catch(Exception)
                                {
                                    if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav"))
                                        File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                }
                                TextWriter write = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                write.WriteLine(line1);
                                write.WriteLine(line2);
                                write.WriteLine("GrenadLauncher");
                                write.Close();
                            }

                        if (Bayonet[x] && gameManager.Level >= BayonetLevel[x])
                            if (GUILayout.Button("Bayonet", GUILayout.Width(150)))
                            {
                                GameObject.FindWithTag("Player").GetComponent<Gun>().currentTactic = Gun.tactical.Bayonet;
                                string line1 = "" , line2 = "";
                                try
                                {
                                    TextReader read = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                    line1 = read.ReadLine();
                                    line2 = read.ReadLine();
                                    read.Close();
                                }
                                catch(Exception)
                                {
                                    if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav"))
                                        File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                }
                                TextWriter write = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                write.WriteLine(line1);
                                write.WriteLine(line2);
                                write.WriteLine("Bayonet");
                                write.Close();
                            }


                        GUILayout.Label("Camo");
                        if (GUILayout.Button("Default", GUILayout.Width(150)))
                        {
                            string line1 = "", line2 = "";
                            try
                            {
                                TextReader read = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                read.ReadLine();
                                line1 = read.ReadLine();
                                line2 = read.ReadLine();
                                read.Close();
                            }
                            catch (Exception)
                            {
                                if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav"))
                                    File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                            }
                            TextWriter write = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                            write.WriteLine("");
                            write.WriteLine(line1);
                            write.WriteLine(line2);
                            write.Close();


                            if (GameObject.FindWithTag("Player") != null)
                                Destroy(GameObject.FindWithTag("Player"));

                            Instantiate(GunToSpawn[x], GunSpawner.transform.position, Quaternion.identity);
                            rotationX = 0;
                        }

                        for (int c = 0; c < camo.Length; c++)
                        {
                            if (gameManager.Level >= CamoLevels[c])
                                if (GUILayout.Button(camo[c].name, GUILayout.Width(150)))
                                {
                                    string line1 = "", line2 = "";
                                    try
                                    {
                                        TextReader read = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                        read.ReadLine();
                                        line1 = read.ReadLine();
                                        line2 = read.ReadLine();
                                        read.Close();
                                    }
                                    catch (Exception)
                                    {
                                        if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav"))
                                            File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                    }
                                    TextWriter write = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + Wepones[x].ToString() + ".sav");
                                    write.WriteLine(camo[c].name);
                                    write.WriteLine(line1);
                                    write.WriteLine(line2);
                                    write.Close();

                                    GameObject.FindWithTag("Player").gameObject.GetComponent<Gun>().currentCamo = camo[c].name;
                                }
                        }
                    }
                }

                switch (weaponEditor)
                {
                    case "":
                        if (GUILayout.Button("Primary", GUILayout.Width(150)))
                        {
                            weaponEditor = "P";
                        }

                        if (GUILayout.Button("Secondary", GUILayout.Width(150)))
                        {
                            weaponEditor = "S";
                        }
                        break;

                    case "P":
                        foreach (string c in PrimarywepTypes)
                        {
                            if (GUILayout.Button(c, GUILayout.Width(150)))
                            {
                                weaponEditor = c;
                            }
                        }
                        break;
                    case "S":
                        foreach (string c in SecondaryWepTypes)
                        {
                            if (GUILayout.Button(c, GUILayout.Width(150)))
                            {
                                weaponEditor = c;
                            }
                        }
                        break;
                }

                GameObject.Find("Camera").GetComponent<Camera>().fieldOfView = GUI.HorizontalScrollbar(new Rect(0, Screen.height - 170, Screen.width - 230, Screen.height - 150), GameObject.Find("Camera").GetComponent<Camera>().fieldOfView, 1, 20, 70);

                if (Input.GetButton("Fire1") && GameObject.FindWithTag("Player") != null)
                {
                    rotationX += Input.GetAxis("Mouse X");
                    rotationX = Mathf.Clamp(rotationX, -180, 180);
                    GameObject.FindWithTag("Player").gameObject.transform.localEulerAngles = new Vector3(0, rotationX, 0);
                }
                wepCam.GetComponent<Camera>().Render();
                break;
            case "Options":
                GUILayout.BeginHorizontal();
                //first column
                GUILayout.BeginVertical();
                if (GUILayout.Button("Resolution"))
                    Options = "Resolution";

                if (GUILayout.Button("Audio"))
                    Options = "Audio";

                if (GUILayout.Button("Video"))
                    Options = "Video";

                if (GUILayout.Button("Gameplay"))
                    Options = "Gameplay";
                GUILayout.EndVertical();
                GUILayout.BeginVertical();
                //second column
                switch (Options)
                {
                    case "Resolution":
                        fullScreen = GUILayout.Toggle(fullScreen, "Fullscreen");
                        for (int x = 0; x < Screen.resolutions.Length; x++)
                        {
                            if (Screen.resolutions[x].height > 764)
                            {
                                if (GUILayout.Button(Screen.resolutions[x].width + "X" + Screen.resolutions[x].height))
                                {
                                    res = x;
                                    resPic = true;
                                    Screen.SetResolution(Screen.resolutions[x].width, Screen.resolutions[x].height, fullScreen);
                                }
                            }
                        }
                        break;
                    case "Audio":
                        GUILayout.Label("Master Volume");
                        gameManager.masterVol = GUILayout.HorizontalScrollbar(gameManager.masterVol, 0.01f, 0.0f, 1.0f);
                        break;
                    case "Video":
                        int fixer = 0;
                        gameManager.softParticles = GUILayout.Toggle(gameManager.softParticles, "Soft Particles");
                        GUILayout.Label("Shadows");
                        gameManager.shadowes = GUILayout.HorizontalScrollbar(gameManager.shadowes, 10, 250, 800);
                        GUILayout.Label("Texture Quality");
                        fixer = 0;
                        for (int c = 0; c < texture.Length; c++)
                        {
                            if (texture[c])
                                fixer++;
                        }
                        if (fixer == 1)
                        {
                            if (texture[3])
                                texture[3] = GUILayout.Toggle(texture[3], "Low");

                            if (texture[2])
                                texture[2] = GUILayout.Toggle(texture[2], "Simple");

                            if (texture[1])
                                texture[1] = GUILayout.Toggle(texture[1], "Good");

                            if (texture[0])
                                texture[0] = GUILayout.Toggle(texture[0], "High");
                        }
                        else
                        {
                            texture[3] = GUILayout.Toggle(texture[3], "Low");
                            texture[2] = GUILayout.Toggle(texture[2], "Simple");
                            texture[1] = GUILayout.Toggle(texture[1], "Good");
                            texture[0] = GUILayout.Toggle(texture[0], "High");
                        }

                        if (texture[0])
                            gameManager.textureQuality = 1;
                        else if (texture[1])
                            gameManager.textureQuality = 2;
                        else if (texture[2])
                            gameManager.textureQuality = 3;
                        else if (texture[3])
                            gameManager.textureQuality = 4;
                        GUILayout.Label("Pixel Light Count");
                        fixer = 0;
                        for (int c = 0; c < pixelLight.Length; c++)
                        {
                            if (pixelLight[c])
                                fixer++;
                        }
                        if (fixer == 1)
                        {
                            if (pixelLight[0])
                                pixelLight[0] = GUILayout.Toggle(pixelLight[0], "Low");

                            if (pixelLight[1])
                                pixelLight[1] = GUILayout.Toggle(pixelLight[1], "Simple");

                            if (pixelLight[2])
                                pixelLight[2] = GUILayout.Toggle(pixelLight[2], "Good");

                            if (pixelLight[3])
                                pixelLight[3] = GUILayout.Toggle(pixelLight[3], "High");

                            if (pixelLight[4])
                                pixelLight[4] = GUILayout.Toggle(pixelLight[4], "Very High");
                        }
                        else
                        {
                            pixelLight[0] = GUILayout.Toggle(pixelLight[0], "Low");
                            pixelLight[1] = GUILayout.Toggle(pixelLight[1], "Simple");
                            pixelLight[2] = GUILayout.Toggle(pixelLight[2], "Good");
                            pixelLight[3] = GUILayout.Toggle(pixelLight[3], "High");
                            pixelLight[4] = GUILayout.Toggle(pixelLight[4], "Very High");
                        }
                        if (pixelLight[0])
                            gameManager.pixelLightCount = 1;
                        else if (pixelLight[1])
                            gameManager.pixelLightCount = 2;
                        else if (pixelLight[2])
                            gameManager.pixelLightCount = 3;
                        else if (pixelLight[3])
                            gameManager.pixelLightCount = 4;
                        else if (pixelLight[4])
                            gameManager.pixelLightCount = 5;
                        GUILayout.Label("Anti Aliasing");
                        
                        fixer = 0;
                        for (int c = 0; c < antiAliasing.Length; c++)
                        {
                            if (antiAliasing[c])
                                fixer++;
                        }
                        if (fixer == 1)
                        {
                            if (antiAliasing[0])
                                antiAliasing[0] = GUILayout.Toggle(antiAliasing[0], "none");

                            if (antiAliasing[1])
                                antiAliasing[1] = GUILayout.Toggle(antiAliasing[1], "2x");

                            if (antiAliasing[2])
                                antiAliasing[2] = GUILayout.Toggle(antiAliasing[2], "4x");

                            if (antiAliasing[3])
                                antiAliasing[3] = GUILayout.Toggle(antiAliasing[3], "8x");
                        }
                        else
                        {
                            antiAliasing[0] = GUILayout.Toggle(antiAliasing[0], "none");
                            antiAliasing[1] = GUILayout.Toggle(antiAliasing[1], "2x");
                            antiAliasing[2] = GUILayout.Toggle(antiAliasing[2], "4x");
                            antiAliasing[3] = GUILayout.Toggle(antiAliasing[3], "8x");
                        }
                        if (texture[0])
                            gameManager.antiAliasing = 0;
                        else if (antiAliasing[1])
                            gameManager.antiAliasing = 2;
                        else if (antiAliasing[2])
                            gameManager.antiAliasing = 4;
                        else if (antiAliasing[3])
                            gameManager.antiAliasing = 8;
                        break;
                    case "Gameplay":
                        GUILayout.Label("Mouse Senstivity");
                        gameManager.mouseSenstivity = GUILayout.HorizontalScrollbar(gameManager.mouseSenstivity, 1, 3, 15);
                        break;
                }
                GUILayout.EndVertical();
                GUILayout.EndHorizontal();
                break;
        }
    }
}